<template>
  <!--  修改学生-->
  <div style="margin-top: 60px;margin-left:330px;width: 500px;height: 500px;border: 0 solid red;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="学号" prop="number">
        <el-input v-model="ruleForm.number"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <div style="width: 170px;height: 30px;">
          <template>
            <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
            <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
          </template>
        </div>
      </el-form-item>

      <el-form-item label="学院" prop="college">
        <el-input v-model="ruleForm.college"></el-input>
      </el-form-item>

      <el-form-item label="专业" prop="major">
        <el-input v-model="ruleForm.major"></el-input>
      </el-form-item>

      <el-form-item label="班级" prop="classname">
        <el-input v-model="ruleForm.classname"></el-input>
      </el-form-item>

      <el-form-item label="联系方式" prop="contact">
        <el-input v-model="ruleForm.contact"></el-input>
      </el-form-item>


      <el-form-item label="公寓号" prop="dormitoryName">
        <el-select v-model="ruleForm.dormitoryId" placeholder="请选择公寓">
          <el-option v-for="item in dormitoryList" :label="item.name" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <!-- 下划线分隔 -->
      <div style="border-top: 1px solid #ccc; margin-top: 20px; padding-top: 20px;">

        <el-form-item label="运动习惯" prop="exerciseHabit">

          <template>
            <el-radio v-model="ruleForm.exerciseHabit" label="活跃型">活跃型</el-radio>
            <el-radio v-model="ruleForm.exerciseHabit" label="一般型">一般型</el-radio>
            <el-radio v-model="ruleForm.exerciseHabit" label="懒惰型">懒惰型</el-radio>
            <el-radio v-model="ruleForm.exerciseHabit" label="不规律型">不规律型</el-radio>
          </template>

        </el-form-item>

        <el-form-item label="住校经历" prop="boardingExperience">
          <template>
            <el-radio v-model="ruleForm.boardingExperience" label="有住校经历且适应良好">有住校经历且适应良好</el-radio>
            <el-radio v-model="ruleForm.boardingExperience" label="有住校经历但不适应">有住校经历但不适应</el-radio>
            <el-radio v-model="ruleForm.boardingExperience" label="无住校经历但愿意尝试">无住校经历但愿意尝试</el-radio>
            <el-radio v-model="ruleForm.boardingExperience" label="无住校经历且不愿意尝试">无住校经历且不愿意尝试</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="卫生习惯" prop="hygieneHabit">
          <template>
            <el-radio v-model="ruleForm.hygieneHabit" label="整洁型">整洁型</el-radio>
            <el-radio v-model="ruleForm.hygieneHabit" label="一般型">一般型</el-radio>
            <el-radio v-model="ruleForm.hygieneHabit" label="随性型">随性型</el-radio>
            <el-radio v-model="ruleForm.hygieneHabit" label="极端型">极端型</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="性格特点" prop="personalityTraits">
          <template>
            <el-radio v-model="ruleForm.personalityTraits" label="外向型">外向型</el-radio>
            <el-radio v-model="ruleForm.personalityTraits" label="内向型">内向型</el-radio>
            <el-radio v-model="ruleForm.personalityTraits" label="理智型">理智型</el-radio>
            <el-radio v-model="ruleForm.personalityTraits" label="感性型">感性型</el-radio>
          </template>
        </el-form-item>


      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  data() {
    return {
      i: 0,
      category: null,
      ruleForm: {
        number: '',
        name: '',
        gender: '男',
        dormitoryName: '',
        sleepingHabit: '',
        financialCondition: '',
        exerciseHabit: '',
        boardingExperience: '',
        hygieneHabit: '',
        personalityTraits: '',
        college: '',
        major: '',
        classname: '',
        contact: '',
      },
      dormitoryList: '',
      rules: {
        number: [
          {required: true, message: '请输入学号', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请输入性别', trigger: 'blur'}
        ],
        dormitoryId: [
          {required: true, message: '请选择公寓', trigger: 'change'}
        ],
        sleepingHabit: [
          {required: true, message: '请选择作息习惯', trigger: 'change'}
        ],
        financialCondition: [
          {required: true, message: '请选择经济条件', trigger: 'change'}
        ],
        exerciseHabit: [
          {required: true, message: '请选择运动习惯', trigger: 'change'}
        ],
        boardingExperience: [
          {required: true, message: '请选择住校经历', trigger: 'change'}
        ],
        hygieneHabit: [
          {required: true, message: '请选择个人卫生习惯', trigger: 'change'}
        ],
        personalityTraits: [
          {required: true, message: '请选择性格特点', trigger: 'change'}
        ],
        college: [
          {required: true, message: '请输入学院', trigger: 'blur'}
        ],
        major: [
          {required: true, message: '请输入专业', trigger: 'blur'}
        ],
        classname: [
          {required: true, message: '请输入班级', trigger: 'blur'}
        ],
        contact: [
          {required: true, message: '请输入联系方式', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.put('http://localhost:9090/student/update', _this.ruleForm).then(function (resp) {
            if (resp.data.code === 0) {
              _this.$alert(_this.ruleForm.name + '修改成功', '', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/studentManager')
                }
              });
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this = this
    axios.get('http://localhost:9090/dormitory/availableList').then(function (resp) {
      _this.dormitoryList = resp.data.data
    })
    axios.get('http://localhost:9090/student/findById/' + _this.$route.query.id).then(function (resp) {
      _this.ruleForm = resp.data.data
    })
  }
}
</script>
